package pearson.automation.forms.note;

import java.util.ArrayList;
import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;

import framework.BaseForm;
import framework.elements.Button;
import framework.elements.ElementList;
import framework.elements.Label;
import framework.elements.TextBox;

public class NoteDetailsForm extends BaseForm {
    
    public class Reply {
        public String replyUserName;
        public String replyContents;
        public String replyDate;
    }

    private static Label identifier = new Label(By.className("mark-module"),
            "Note Details");
    
    private String titleTemplate        = "%1$s//p[@class='mark-title']";
    private String authorTemplate       = "%1$s//h2[@class='user-name']";
    private String sectionNoTemplate    = "%1$s//p[@class='mark-position']";
    private String sectionTemplate      = "%1$s//p[@class='mark-section']";
    
    private String replyTemplate     = "%1$s//a[text()='Add a Reply']";
    private String viewTagsTemplate  = "%1$s//a[text()='View Tags']";
    private String hideTagsTemplate  = "%1$s//a[text()='Hide Tags']";
    private String viewRepliesTemplate  = "%1$s//a[text()='View Replies']";
    private String hideRepliesTemplate  = "%1$s//a[text()='Hide Replies']";
    private String submitTemplate       = "%1$s//input[@value='Reply']";
    private String replyInputTemplate   = "%1$s//textarea[@id='text']";
    private String tagsInputTemplate   = "%1$s//input[@class='textboxlist-bit-editable-input']";

    private String nRepliesTemplate  = "%1$s//span[@class='number-of-replies']";
    
    private String tagsTemplate  = "%1$s//li[contains(@class,'textbox-bit-box')]";
    private String replyUserTemplate  =
        "%1$s//section[@class='mark-replies']//h2[@class='user-name']";
    private String replyContentsTemplate  =
        "%1$s//section[@class='mark-replies']//p[@class='mark-title']";
    private String replyDateTemplate  =
        "%1$s//section[@class='mark-replies']//p[@class='mark-date']";
    
    private Label title =
        new Label(By.xpath(String.format(titleTemplate, "")), "Title");
    private Label  author =
        new Label(By.xpath(String.format(authorTemplate, "")), "Author");
    private Label  sectionNo =
        new Label(By.xpath(String.format(sectionNoTemplate, "")),
                "Section Abbreviation");
    private Label  section   =
        new Label(By.xpath(String.format(sectionTemplate, "")),
                "Section Title");
    
    private Button reply    =
        new Button(By.xpath(String.format(replyTemplate, "")),    "Add a Reply");
    private Button viewTags =
        new Button(By.xpath(String.format(viewTagsTemplate, "")), "View Tags");
    private Button hideTags =
        new Button(By.xpath(String.format(hideTagsTemplate, "")), "Hide Tags");
    
    private Button viewReplies =
        new Button(By.xpath(String.format(viewRepliesTemplate, "")), "View Replies");
    private Button hideReplies =
        new Button(By.xpath(String.format(hideRepliesTemplate, "")), "Hide Replies");
    private TextBox replyInput =
        new TextBox(By.xpath(String.format(replyInputTemplate, "")), "Reply Input");
    private Button submit =
        new Button(By.xpath(String.format(submitTemplate, "")),    "Submit");
    private Label  numberOfReplies   =
        new Label(By.xpath(String.format(nRepliesTemplate, "")),  "Replies");
    
    private ElementList tags =
        new ElementList(By.xpath(String.format(tagsTemplate, "")), "Tags");
    private TextBox tagsInput =
        new TextBox(By.xpath(String.format(tagsInputTemplate, "")), "Tag Input");
    private ElementList replyUser =
        new ElementList(By.xpath(String.format(replyUserTemplate, "")),
                "Reply User");
    private ElementList replyContents =
        new ElementList(By.xpath(String.format(replyContentsTemplate, "")),
                "Reply Contents");
    private ElementList replyDate =
        new ElementList(By.xpath(String.format(replyDateTemplate, "")),
                "Reply Date");
    
    private Button delete   =
        new Button(By.linkText("Delete"),      "Delete");
    private Button close    =
        new Button(By.linkText("Close"),       "Close");

    
    private String xPathTemp;
    
    public NoteDetailsForm() {
        super(identifier.getLocator(), "Note Details");
    }
    
    public NoteDetailsForm(String dataMarkRef) {
        super(identifier.getLocator(), "Note Details");
        
        xPathTemp = String.format(
                "//div[@data-mark-ref='%s']", dataMarkRef);
        title  = new Label(By.xpath(String.format(
                titleTemplate, xPathTemp)), "Title");
        author = new Label(By.xpath(String.format(
                authorTemplate, xPathTemp)), "Author");
        sectionNo = new Label(By.xpath(String.format(
                sectionNoTemplate, xPathTemp)), "Section Abbreviation");
        section = new Label(By.xpath(String.format(
                sectionTemplate, xPathTemp)), "Section Title");
        
        reply = new Button(By.xpath(String.format(
                replyTemplate, xPathTemp)), "Add a Reply");
        viewTags = new Button(By.xpath(String.format(
                viewTagsTemplate, xPathTemp)), "View Tags");
        hideTags = new Button(By.xpath(String.format(
                hideTagsTemplate, xPathTemp)), "Hide Tags");
        viewReplies = new Button(By.xpath(String.format(
                viewRepliesTemplate, xPathTemp)), "View Tags");
        hideReplies = new Button(By.xpath(String.format(
                hideRepliesTemplate, xPathTemp)), "Hide Tags");
        numberOfReplies = new Label(By.xpath(String.format(
                nRepliesTemplate, xPathTemp)),  "Replies");
        tags = new ElementList(By.xpath(String.format(
                tagsTemplate, xPathTemp)), "Tags");
        replyUser = new ElementList(By.xpath(String.format(
                replyUserTemplate, xPathTemp)), "Tags");
        replyContents = new ElementList(By.xpath(String.format(
                replyContentsTemplate, xPathTemp)), "Tags");
        replyDate = new ElementList(By.xpath(String.format(
                replyDateTemplate, xPathTemp)), "Tags");
        submit = new Button(By.xpath(String.format(
                submitTemplate, xPathTemp)), "Submit");
        replyInput = new TextBox(By.xpath(String.format(
                replyInputTemplate, xPathTemp)), "Submit");
        tagsInput = new TextBox(By.xpath(String.format(
                tagsInputTemplate, xPathTemp)), "Tag Input");
    }

    public String getAuthor() {
        return author.getText();
    }

    public String getTitle() {
        return title.getText();
    }

    public String getSection() {
        return section.getText();
    }

    public String getSectionNo() {
        return sectionNo.getText();
    }
    
    public void clickClose() {
        close.clickAndWait();
        title.waitForElementDissapear();
    }
    
    public void clickViewTags() {
        viewTags.clickAndWait();
    }
    
    public void clickHideTags() {
        hideTags.clickAndWait();
    }
    
    public void clickViewReplies() {
        viewReplies.clickAndWait();
    }
    
    public void clickHideReplies() {
        hideReplies.clickAndWait();
    }
   
    public void addTag(String tags) {
        viewTags.clickAndWait();
        tagsInput.setText(tags);
        tagsInput.getElement().sendKeys(Keys.ENTER);
        hideTags.clickAndWait();
    }
    
    public void addReply(String replyText) {
        reply.clickAndWait();
        replyInput.setText(replyText);
        submit.clickAndWait();
        replyInput.waitForElementToBeLoaded();
    }
    
    public List<String> getTagList() {
        return tags.getListElementTexts();
    }
    
    public int getNumberOfReplies() {
        if (!numberOfReplies.isPresent(1)) {
            return 0;
        } else {
            try {
                return Integer.parseInt(numberOfReplies.getText());
            } catch (NumberFormatException e) {
                fatal(String.format("Cannot convert %s to integer",
                        numberOfReplies.getText()));
                return 0;
            }
        }
    }
    
    public ArrayList<Reply> getReplies() {
        ArrayList<Reply> returnList = new ArrayList<Reply>();

        List<String> replyContentsText = replyContents.getListElementTexts();
        List<String> replyUserText = replyUser.getListElementTexts();
        List<String> replyDateText = replyDate.getListElementTexts();
        
        int listLength = replyContentsText.size();
        for (int i = 0; i < listLength; i++) {
            Reply reply = new Reply();
            reply.replyContents = replyContentsText.get(i);
            reply.replyDate = replyDateText.get(i);
            reply.replyUserName = replyUserText.get(i);
            returnList.add(reply);
        }
        return returnList;
    }

}
